<?php
/**
 *  默认主页
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Index as IndexModel;
use app\admin\model\ModelM as ModelMModel;

class Index extends Common
{
	protected $model;
	public function _initialize()
    {
		parent::_initialize();
		$this->model=new IndexModel();

	}
    // 主页面
    public function index()
    {
        $this->assign('server', get_server_info());
        $this->assign('CORE_VERSION', 'Hncaobang');
        $this->assign('RELEASE_TIME','2019-1-6');
		$this->assign('revise','2.0');
		return $this->fetch();
    }
    // 用户中心，修改密码
    public function ucenter()
    {
         if($this->request->isPost()){
            $username = input('username/s'); // 用户名
            $realname = input('realname/s'); // 真实姓名
            $cpassword = input('cpassword/s'); // 现在密码
            $password = input('password/s'); // 新密码
            $rpassword = input('rpassword/s'); // 确认密码
            
            if (! $username) {
                alert('用户名不能为空！','back');
            }
            if (! $cpassword) {
                alert('当前密码不能为空！','back');
            }
            
            if (! preg_match('/^[\x{4e00}-\x{9fa5}\w\-\.@]+$/u', $username)) {
                alert('用户名含有不允许的特殊字符！','back');
            }
            
            $data = array(
                'username' => $username,
                'realname' => $realname,
                'update_user' => $username,
				'update_time' => date('Y-m-d H:i:s')
            );
            
            // 如果有修改密码，则添加数据
            if ($password) {
                if ($password != $rpassword) {
                    alert('确认密码不正确！','back');
                }
                $data['password'] = encrypt_string($password);
            }
            
            // 检查现有密码
            if ($this->model->checkUserPwd(encrypt_string($cpassword))) {
                if ($this->model->modUserInfo($data)) {
                    session('username', input('username/s'));
                    session('realname', input('realname/s'));
                    $this->success('用户资料修改成功！');
                }
            } else {
                alert('当前密码错误！','back');
            }
        }
		$info=array();
		$info['username']=$_SESSION['admin']['username'];
		$info['realname']=$_SESSION['admin']['realname'];
		$this->assign('session', $info);
		return $this->fetch();
    }
    // 切换显示的数据区域
    public function area()
    {
         if($this->request->isPost()) {
            $acode = post('acode');
            if (in_array($acode, session('acodes'))) {
                session('acode', $acode);
            }
            location(- 1);
        }
    }
    // 清理缓存
    public function clearCache()
    {
		if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            alert('清理缓存成功！','back');
        } else {
           alert('清理缓存失败！','back');
        }

    }
    // 文件上传方法
    public function upload()
    {
        $upload = upload('upload','jpg,png,gif');
        if (is_array($upload)) {
            //json(1, $upload);
			return json(array('code'=>'1','data'=>$upload));
        } else {
			return json(array('code'=>'0','data'=>$upload));
        }
    }
	// ico图片上传
    public function uploadico()
    {
        $upload = upload('upload','ico','32','32',false);
        if (is_array($upload)) {
			return json(array('code'=>'1','data'=>$upload));
        } else {
           return json(array('code'=>'0','data'=>$upload));
        }
    }
}